package com.wdd.myplatform.controller;

import com.wdd.myplatform.common.BaseResult;
import com.wdd.myplatform.entity.EsDemoBean;
import com.wdd.myplatform.repository.EsGoodsRespository;
import com.wdd.myplatform.service.EsDemoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * elasticSearch测试
 * @author Administrator
 */
@RestController
@RequestMapping("/esController")
@Api("elasticSearch测试")
public class EsController {

    @Autowired
    private EsDemoService esDemoService;

    @Autowired
    private EsGoodsRespository esGoodsRespository;

    @ApiOperation(value = "根据id创建商品")
    @GetMapping(value = "/create/{id}")
    public BaseResult<EsDemoBean> create(@PathVariable String id) {
        EsDemoBean esDemoBean = esDemoService.create(id);
        if (esDemoBean != null) {
            return BaseResult.success(esDemoBean);
        } else {
            return BaseResult.error();
        }
    }

    @ApiOperation(value = "根据id查询商品")
    @GetMapping(value = "/get/{id}")
    public BaseResult<EsDemoBean> get(@PathVariable String id) {
        EsDemoBean esDemoBean = esGoodsRespository.getByUserId(id);
        if (esDemoBean != null) {
            return BaseResult.success(esDemoBean);
        } else {
            return BaseResult.error();
        }
    }

    @ApiOperation(value = "根据id查询商品")
    @GetMapping(value = "/delete/{id}")
    public void delete(@PathVariable String id) {
        EsDemoBean esDemoBean = new EsDemoBean();
        esDemoBean.setUserId(id);
        esGoodsRespository.delete(esDemoBean);
    }

    @ApiOperation(value = "简单搜索")
    @GetMapping(value = "/search/page")
    public BaseResult<Page<EsDemoBean>> searchPage(@RequestParam(required = false) String keyword,
                                               @RequestParam(required = false, defaultValue = "0") Integer pageNum,
                                               @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        Page<EsDemoBean> EsDemoBeanPage = esDemoService.search(keyword, pageNum, pageSize);
        return BaseResult.success(EsDemoBeanPage);
    }

    @ApiOperation(value = "简单搜索")
    @GetMapping(value = "/search/list")
    public BaseResult<List<EsDemoBean>> searchList(@RequestParam(required = false) String keyword) {
        List<EsDemoBean> EsDemoBeanPage = esDemoService.searchList(keyword);
        return BaseResult.success(EsDemoBeanPage);
    }

    @ApiOperation(value = "综合搜索、筛选、排序")
    @ApiImplicitParam(name = "sort", value = "排序字段:0->按相关度；1->按新品；2->按销量；3->价格从低到高；4->价格从高到低",
            defaultValue = "0", allowableValues = "0,1,2,3,4", paramType = "query", dataType = "integer")
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    @ResponseBody
    public BaseResult<Page<EsDemoBean>> search(@RequestParam(required = false) String keyword,
                                                      @RequestParam(required = false) Long brandId,
                                                      @RequestParam(required = false) Long productCategoryId,
                                                      @RequestParam(required = false, defaultValue = "0") Integer pageNum,
                                                      @RequestParam(required = false, defaultValue = "5") Integer pageSize,
                                                      @RequestParam(required = false, defaultValue = "0") Integer sort) {
        Page<EsDemoBean> EsDemoBeanPage = esDemoService.search(keyword, brandId, productCategoryId, pageNum, pageSize, sort);
        return BaseResult.success(EsDemoBeanPage);
    }

    @ApiOperation(value = "根据商品id推荐商品")
    @RequestMapping(value = "/recommend/{id}", method = RequestMethod.GET)
    @ResponseBody
    public BaseResult<Page<EsDemoBean>> recommend(@PathVariable String id,
                                                         @RequestParam(required = false, defaultValue = "0") Integer pageNum,
                                                         @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        Page<EsDemoBean> EsDemoBeanPage = esDemoService.recommend(id, pageNum, pageSize);
        return BaseResult.success(EsDemoBeanPage);
    }


}
